﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyThuVien.DTO;
using System.Data;
using QuanLyThuVien.Supporter;
using System.Data.SqlClient;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng TK_MUONSACH
    //----------------------------------------------------------------------
    public class DALTK_MuonSach
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALTK_MuonSach()
        { }

        //----------------------------------------------------------------------
        //Desc: đếm phiếu thống kê
        //----------------------------------------------------------------------
        public int DemPhieuTK(DTOTK_MuonSach tkMuonSach)
        {
            if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spTK_MuonSach_DemPhieuTK";
            SqlParameter sqlprThangTK = new SqlParameter("@ThangTK", SqlDbType.Int) { Value = tkMuonSach.ThangTK };
            SqlParameter sqlprNamTK = new SqlParameter("@NamTK", SqlDbType.Int) { Value = tkMuonSach.NamTK };
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName, sqlprThangTK, sqlprNamTK);
            if (obj == null)
                return -1;
            int dem;
            try
            {
                dem = (int)obj;
            }
            catch
            {
                return -1;
            }

            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật phiếu thống kê
        //----------------------------------------------------------------------
        public bool CapNhat(DTOTK_MuonSach tkMuonSach)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spTK_MuonSach_CapNhatPhieuTK";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprThangTK = new SqlParameter("@ThangTK", SqlDbType.Int) { Value = tkMuonSach.ThangTK };
            SqlParameter sqlprNamTK = new SqlParameter("@NamTK", SqlDbType.Int) { Value = tkMuonSach.NamTK };
            SqlParameter sqlprTongSoLuotMuon = new SqlParameter("@TongSoLuotMuon", SqlDbType.Int)
            { Value = tkMuonSach.TongSoLuotMuon };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprThangTK, sqlprNamTK, sqlprTongSoLuotMuon);
        }

        //----------------------------------------------------------------------
        //Desc: lấy dữ liệu thống kê
        //----------------------------------------------------------------------
        public bool LayDuLieuTK(ref DTOTK_MuonSach tkMuonSach)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spTK_MuonSach_LayPhieuTK";
            SqlParameter sqlprThangTK = new SqlParameter("@ThangTK", SqlDbType.Int) { Value = tkMuonSach.ThangTK };
            SqlParameter sqlprNamTK = new SqlParameter("@NamTK", SqlDbType.Int) { Value = tkMuonSach.NamTK };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprThangTK, sqlprNamTK);
            if (data == null || data.Rows.Count == 0)
                return false;
            try
            {
                tkMuonSach.TongSoLuotMuon = long.Parse(data.Rows[0]["TongSoLuotMuon"].ToString());
            }
            catch
            { return false; }

            return true;
        }
    }
}
